package br.com.AC.bean;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;

import br.com.AC.entidades.Imc;
import br.com.AC.modelo.PacienteModel;
   
public class ChartBean implements Serializable {  
  
    PacienteModel pm = new PacienteModel();
    
	private CartesianChartModel categoryModel;  
	  
    public ChartBean() {  
    }  
  
    public CartesianChartModel getCategoryModel() {  
        return categoryModel;  
    }  
  
    public String atualizaImc()
    {
    	createCategoryModel();
    	return "atualizaImc";
    }
    
    public CartesianChartModel createCategoryModel() {  
		SimpleDateFormat formDate = new SimpleDateFormat("dd/MM/yyyy");

		FacesContext contexto = FacesContext.getCurrentInstance();
		HttpSession sessao = (HttpSession) contexto.getExternalContext().getSession(false);
		
		Integer id = (Integer)sessao.getAttribute("idU");
		
    	List<Imc> lista =new ArrayList<Imc>(); 
    	lista = pm.pesqImc(id);
    	
    	categoryModel = new CartesianChartModel();  
        
        ChartSeries imc = new ChartSeries();  
        imc.setLabel(pm.pesqUsu(id)); 
        for(int i = 0; i<lista.size(); i++)
        {
	        double calc = lista.get(i).getPeso()/(lista.get(i).getAltura()*lista.get(i).getAltura());
        	
        	imc.set(formDate.format(lista.get(i).getDtAtul()), calc);  
        }
        
	     categoryModel.addSeries(imc);  
	    
	     return categoryModel;
	    
    }
	  
  
}